package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinlejia.erp.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/4 0004.
 * 协作申请详情--分成信息adapter
 */

public class AssistInfoAdapter extends BaseAdapter{
    private LayoutInflater mInflater;
    private ArrayList<Map<String,Object>> data;
    private Map<String,String> typeMap = new HashMap<>();
    private Context context;

    public AssistInfoAdapter(Context context, ArrayList<Map<String,Object>> d) {
        this.mInflater = LayoutInflater.from(context);
        this.data = d;
        this.context = context;

    }
    public void setData(ArrayList<Map<String,Object>> d){
        this.data = d;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_assist_info, null);
            holder = new ViewHolder();
            holder.tvInitiator = ((TextView) convertView.findViewById(R.id.tv_initiator));
            holder.etInitiator = ((TextView) convertView.findViewById(R.id.et_initiator));
            holder.tvPercentage = ((TextView) convertView.findViewById(R.id.tv_percentage));
            holder.etPercentage = ((TextView) convertView.findViewById(R.id.et_percentage));
            holder.tvNumber = ((TextView) convertView.findViewById(R.id.tv_number));
            holder.etNumber = ((TextView) convertView.findViewById(R.id.et_number));
            holder.tvIsParticipates = ((TextView) convertView.findViewById(R.id.tv_isParticipates));
            holder.etIsParticipates = ((TextView) convertView.findViewById(R.id.et_isParticipates));
            holder.tvIsShure = ((TextView) convertView.findViewById(R.id.tv_isShure));
            holder.etIsShure = ((TextView) convertView.findViewById(R.id.et_isShure));
            holder.parent = ((LinearLayout) convertView.findViewById(R.id.parent));
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Map<String, Object> map = data.get(position);

        if(0==position){//发起人
            holder.tvInitiator.setText("发起人");

        }else{//协作人
            holder.tvInitiator.setText("协作人");
        }
        //协助人名字
        if(map.containsKey("name")&&null!=map.get("name")){
            holder.etInitiator.setText(map.get("name").toString());
        }
        if(map.containsKey("ratio")&&null!=map.get("ratio")){
            holder.etPercentage.setText(map.get("ratio").toString());
        }
        if(map.containsKey("edgeNum")&&null!=map.get("edgeNum")){
            holder.etNumber.setText(map.get("edgeNum").toString());
        }
        if(map.containsKey("isJoin")&&null!=map.get("isJoin")){
            if("0".equals(map.get("isJoin").toString())){
                holder.etIsParticipates.setText("不参与分成");
            }else{
                holder.etIsParticipates.setText("参与分成");
            }
        }
        if(map.containsKey("userState")&&null!=map.get("userState")){
            if("0".equals(map.get("userState").toString())){
                holder.etIsShure.setText("待确认");
            }else{
                holder.etIsShure.setText("已确认");
            }
        }
        if(map.containsKey("pin") && !"".equals(map.get("pin"))){
            changeColor(holder);
        }else{
            changeColorNormal(holder);
        }
        return convertView;
    }

    /**
     * 存放控件
     */
    public final class ViewHolder {
        public TextView tvInitiator;//发起人
        public TextView etInitiator;
        public TextView tvPercentage;//分成占比
        public TextView etPercentage;
        public TextView tvNumber;//边数
        public TextView etNumber;
        public TextView tvIsParticipates;//是否参与
        public TextView etIsParticipates;
        public TextView tvIsShure;//确认状态
        public TextView etIsShure;
        public LinearLayout parent;

    }

    private void changeColorNormal(ViewHolder holder) {
        holder.tvInitiator.setTextColor(context.getResources().getColor(R.color.colorTip));
        holder.etInitiator.setTextColor(context.getResources().getColor(R.color.textColor_333));
        holder.tvPercentage.setTextColor(context.getResources().getColor(R.color.colorTip));
        holder.etPercentage.setTextColor(context.getResources().getColor(R.color.textColor_333));
        holder.tvNumber.setTextColor(context.getResources().getColor(R.color.colorTip));
        holder.etNumber.setTextColor(context.getResources().getColor(R.color.textColor_333));
        holder.tvIsParticipates.setTextColor(context.getResources().getColor(R.color.colorTip));
        holder.etIsParticipates.setTextColor(context.getResources().getColor(R.color.textColor_333));
        holder.tvIsShure.setTextColor(context.getResources().getColor(R.color.colorTip));
        holder.etIsShure.setTextColor(context.getResources().getColor(R.color.textColor_333));
        holder.parent.setBackgroundColor(context.getResources().getColor(R.color.white));
    }


    private void changeColor(ViewHolder holder) {
        holder.tvInitiator.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.etInitiator.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.tvPercentage.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.etPercentage.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.tvNumber.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.etNumber.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.tvIsParticipates.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.etIsParticipates.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.tvIsShure.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.etIsShure.setTextColor(context.getResources().getColor(R.color.hintColor));
        holder.parent.setBackgroundColor(context.getResources().getColor(R.color.windowBg_gray));
    }
}
